//协议
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<jsoncpp/json/json.h>

// #define MySelf 1

const string blank_space_sep = " ";
const string protocol_sep = "\n";

string Encode(string &content)  //content正文部分
{
    string package = to_string(content.size()); //先添加len
    package += protocol_sep; //加反斜杠n
    package += content;
    package += protocol_sep; //字符串末尾加上反斜杠n

    return package;
}

bool Decode(string &package, string *content)
{
    size_t pos = package.find(protocol_sep);
    if(pos == string::npos) return false;
    string len_str = package.substr(0, pos);
    size_t len = stoi(len_str);
    size_t total_len = len_str.size() + len + 2;

    if (package.size() <= total_len) return false;
    *content = package.substr(pos + 1, len);

    package.erase(0, total_len);
    
    return true;
}

//json, protobuf。可以帮助自动序列化发序列化， 也就是线程的序列化反序列化方案。 
class Request
{
public:
    Request(){}
    Request(int data1, int data2, char oper)
        : x(data1), y(data2), op(oper)
    {

    }


    bool serialize(string *out)
    {
#ifdef MySelf

        string s = to_string(x);
        s += blank_space_sep;
        s += op;
        s += blank_space_sep;
        s += to_string(y);

        *out += s;
        return true;
#else
        Json::Value root;
        root["x"] = x;
        root["y"] = y;
        root["op"] = op;
        Json::FastWriter w;
        *out = w.write(root);


#endif

    }

    bool deserialize(string &in)
    {
#ifdef MySelf

        size_t left = in.find(blank_space_sep);
        if (left == string::npos) return false;
        string part_x = in.substr(0, left);
        
        size_t right = in.rfind(blank_space_sep);
        if (right == string::npos) return false;

        string part_y = in.substr(right + 1);
        
        if (left + 2 != right) return false;

        op = in[left + 1];
        x = stoi(part_x);
        y = stoi(part_y);

        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in, root); //反序列化
        x = root["x"].asInt();
        y = root["y"].asInt();
        op = root["op"].asInt();


#endif
    }

    void DebugPrint()
    {
        cout << "构建请求完成: " << x << op << y <<  "?= " << endl;    

    }

public:
    int x;
    int y;
    char op;  // + - * / %
};

class Response
{
public:
    Response(){}
    Response(int res, int c)
        : result(res), code(c)
    {

    }

    bool serialize(string *out)
    {
#ifdef MySelf
        string s = to_string(result);
        s += blank_space_sep;
        s += to_string(code);
    
        *out += s;
        return true;
#else
        Json::Value root;
        root["result"] = result;
        root["code"] = code;    

#endif
    }

    bool deserialize(string &in)
    {
#ifdef MySelf

        size_t pos = in.find(blank_space_sep);
        if (pos == string::npos) return false;
        string part_left = in.substr(0, pos);
        string part_right = in.substr(pos + 1, string::npos);

        result = stoi(part_left);
        code = stoi(part_right);
        return true;
#else
    Json::Value root;
    Json::Reader r;
    r.parse(in, root);
    result = root["result"].asInt();
    code = root["code"].asInt();
#endif
    }
    


public:
    int result;
    int code;  //0， 可信，否则!0具体是几， 表明对应的错误原因。

};